package com.zhengdongzulin.zuke.app.main.apartment

import androidx.lifecycle.rxLifeScope
import com.google.gson.Gson
import com.lxj.androidktx.core.sp
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.StoreDetails
import com.zhengdongzulin.zuke.app.data.UserInfo
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class StoreDetailsVm : BaseViewModel() {

    var get_house_item_id: StateLiveData<StoreDetails> = StateLiveData()

    fun get_house_item_id(id: String?) {
        rxLifeScope.launch(
            {
                var userInfo: UserInfo? = null
                var user = sp().getString("userInfo", "")
                if (!user.isNullOrEmpty()) {
                    userInfo = Gson().fromJson<UserInfo>(user, UserInfo::class.java)
                }
                var r = RxHttp.postJson(Api.GET_HOUSE_ITEM_ID)
                    .add("houseItemId", id)
                    .add("userId", userInfo?.userId)
                    .toResponse<StoreDetails>()
                    .await()
                get_house_item_id.postValueAndSuccess(r)
            }, {
                assignment(it, get_house_item_id)
            }
        )
    }
}